package com.sisSaude.daos;

import java.sql.ResultSet;
import java.util.Vector;

import com.sisSaude.entityBeans.ControleDeFrequenciaCardiaca;
import com.sisSaude.utils.DataProvider;
import com.sisSaude.utils.SqlFactory;

public class ControleDeFrequenciaCardiacaDAO extends GenericDAO<ControleDeFrequenciaCardiaca> {

	// fields
	public static String SELECT_BASICA = " SELECT ControleDeFrequenciaCardiaca.*," +
			                             "        Aluno.nome AS nomeDoAluno, " +
			                             "        Funcionario.nome AS nomeDoAvaliador " +
			                             " FROM ControleDeFrequenciaCardiaca " + 
			                             " INNER JOIN Aluno ON Aluno.id = ControleDeFrequenciaCardiaca.idAluno " +
			                             " INNER JOIN Funcionario ON Funcionario.id = ControleDeFrequenciaCardiaca.idAvaliador " ;
	
	// public methods
	public boolean delete(ControleDeFrequenciaCardiaca controle) {
		String sql = SqlFactory.getSqlDelete("ControleDeFrequenciaCardiaca", "id");
		DataProvider dataProvider = new DataProvider();
		Vector<Object> params = new Vector<Object>();
		params.add(controle.getId());
		try {
			return dataProvider.execute(sql, params);
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		} finally {
			dataProvider.close();
		}
	}
	public ControleDeFrequenciaCardiaca get(int id) {
		ControleDeFrequenciaCardiaca controle = null;
		String sql = SELECT_BASICA + " WHERE ControleDeFrequenciaCardiaca.id = ? "; 
		DataProvider dataProvider = new DataProvider();
		Vector<Object> params = new Vector<Object>();
		params.add(id);
		try {
			dataProvider.select(sql, params);
			if (dataProvider.next()) {
				controle = new ControleDeFrequenciaCardiaca();
				loadAttribs(controle, dataProvider.getResultSet());
			}
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		} finally {
			dataProvider.close();
		}
		return controle;
	}
	public ControleDeFrequenciaCardiaca getUltimoControle(int idAluno) {
		ControleDeFrequenciaCardiaca controle = null;
		String sql = SELECT_BASICA + " WHERE ControleDeFrequenciaCardiaca.idAluno = ? " +
									 " ORDER BY ControleDeFrequenciaCardiaca.dataDaAvaliacao DESC" +
									 " LIMIT 1"; 
		DataProvider dataProvider = new DataProvider();
		Vector<Object> params = new Vector<Object>();
		params.add(idAluno);
		try {
			dataProvider.select(sql, params);
			if (dataProvider.next()) {
				controle = new ControleDeFrequenciaCardiaca();
				loadAttribs(controle, dataProvider.getResultSet());
			}
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		} finally {
			dataProvider.close();
		}
		return controle;
	}
	public void loadAttribs(ControleDeFrequenciaCardiaca controle, ResultSet r) {
		try {
			controle.setId(r.getInt("id"));
			controle.setIdAluno(r.getInt("idAluno"));
			controle.setIdAvaliador(r.getInt("idAvaliador"));
			controle.setDataDaAvaliacao(r.getDate("dataDaAvaliacao"));
			controle.setTaxaAlvo(r.getDouble("taxaAlvo"));
			controle.setBpmAlvo(r.getInt("bpmAlvo"));
			controle.setNomeDoAluno(r.getString("nomeDoAluno"));
			controle.setNomeDoAvaliador(r.getString("nomeDoAvaliador"));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	// protected methods
	protected boolean insert(ControleDeFrequenciaCardiaca controle) {
		String sql = SqlFactory.getSqlInsert("ControleDeFrequenciaCardiaca");
		DataProvider dataProvider = new DataProvider();
		Vector<Object> params = new Vector<Object>();
		params.add(controle.getId());
		params.add(controle.getIdAluno());
	 	params.add(controle.getIdAvaliador());
	 	params.add(controle.getDataDaAvaliacao());
	 	params.add(controle.getTaxaAlvo());
	 	params.add(controle.getBpmAlvo());
	 	try {
			controle.setId(dataProvider.insert(sql, params));
			return controle.getId() > 0;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		} finally {
			dataProvider.close();
		}
	}
	protected boolean update(ControleDeFrequenciaCardiaca controle) {
		String sql = SqlFactory.getSqlUpdate("ControleDeFrequenciaCardiaca", "ControleDeFrequenciaCardiaca.id");
		DataProvider dataProvider = new DataProvider();
		Vector<Object> params = new Vector<Object>();
		params.add(controle.getIdAluno());
	 	params.add(controle.getIdAvaliador());
	 	params.add(controle.getDataDaAvaliacao());
	 	params.add(controle.getTaxaAlvo());
	 	params.add(controle.getBpmAlvo());
		params.add(controle.getId());
		try {
			dataProvider.execute(sql, params);
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		} finally {
			dataProvider.close();
		}
		return true;
	}
}
